package com.xinya.wanandroidclient.ui.articleList

import android.os.Bundle
import android.widget.Toast
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.LinearLayoutManager
import com.xinya.wanandroidclient.R
import com.xinya.wanandroidclient.adapter.ArticleAdapter
import com.xinya.wanandroidclient.base.LazyBaseFragment
import com.xinya.wanandroidclient.bean.ArticleBean
import com.xinya.wanandroidclient.databinding.FragmentArticleListBinding
import com.xinya.wanandroidclient.http.LoadState
import com.xinya.wanandroidclient.view.NavHostFragment

/**
 *
 * @ClassName:      ArticleListFragment$
 * @Description:    MainActivity -> MainFragment -> TabFragment
 * @Author:         xinya
 * @CreateDate:     2021/2/23$ 19:38$
 * @UpdateRemark:   更新内容
 * @Version:        1.0
 */
class ArticleListFragment : LazyBaseFragment() {
    companion object {
        private const val TAG = "ArticleListFragment"
    }

    private var type = 0
    private var tabId = 0

    private val articleAdapter by lazy {
        ArticleAdapter(R.layout.item_article_with_image, null)
    }

    private val articleViewModel by lazy {
        getFragmentVieModel(ArticleViewModel::class.java)
    }

    override fun getLayoutId() = R.layout.fragment_article_list

    private lateinit var dataBinding: FragmentArticleListBinding
    override fun initDataBinding(binding: ViewDataBinding?) {
        dataBinding = binding as FragmentArticleListBinding
    }

    override fun lazyInitView() {
        type = arguments?.getInt("type", 0) ?: 0
        tabId = arguments?.getInt("tabId", 0) ?: 0

        if (type == 0 && tabId == 0) {
            return
        }

        val articleSmartRefreshView = dataBinding.articleSmartRefresh
        val recyclerListView = dataBinding.recyclerList

        // 初始化 articleSmartRefreshView
        articleSmartRefreshView.apply {
            setEnableLoadMore(true)
            setEnableRefresh(true)
            setEnableOverScrollDrag(true)
            autoRefresh()
            setOnRefreshListener {
                // 加载数据
                articleViewModel.getProject(type, tabId, true)
            }
            setOnLoadMoreListener {
                // 加载更多
                articleViewModel.getProject(type, tabId, false)
            }
        }

        recyclerListView.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = articleAdapter
        }
    }

    override fun observer() {
        val articleSmartRefreshView = dataBinding.articleSmartRefresh
        articleViewModel.apply {
            articleLiveData.observe(this@ArticleListFragment) {
                // 获取到文章数据
                if (it.curPage == 1) {
                    articleAdapter.setNewInstance(it.datas)
                } else {
                    articleAdapter.addData(it.datas)
                }
            }
            refreshStateLiveData.observe(this@ArticleListFragment) {
                // 刷新状态
                when (it) {
                    LoadState.Error -> {
                        // smartRefresh数据加载失败
                        smartRefreshState(articleSmartRefreshView, false)
                        Toast.makeText(context, "网络异常", Toast.LENGTH_SHORT).show()
                    }
                    LoadState.Success -> {
                        // smartRefresh数据加载成功
                        smartRefreshState(articleSmartRefreshView, true)
                    }
                    else -> {
                    }
                }
            }
        }
    }

    override fun onClick() {
        articleAdapter.apply {
            setOnItemClickListener { adapter, _, position ->
                val item = adapter.getItem(position) as ArticleBean.Data
                NavHostFragment.findNavController(this@ArticleListFragment)
                    .navigate(R.id.action_main_to_web, Bundle().apply {
                        putString("title", item.title)
                        putInt("id", item.id)
                        putString("url", item.link)
                    })
            }

            setOnItemChildClickListener { adapter, view, position ->
                // item child 点击事件
            }
        }
    }
}